<template>
  <div>
    <el-dialog
      :title="title"
      v-model="visable"
      width="65%"
      :close-on-click-modal="false"
      :draggable="true"
      style="max-width: 950px;"
    >
      <div class="bs-dialog">
        <el-form
          ref="addRef"
          :model="dialogFormData"
          :rules="dialogFormRules"
          label-width="auto"
          style="width: 95%;padding-left: 5%;text-align: left;"
        >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="练习账号:" prop="studentID">
              <el-select
                :disabled="true"
                filterable
                v-model="dialogFormData.studentID"
                placeholder="请选择练习账号" 
                clearable
              >
                <el-option
                  v-for="item in studentList"
                  :key="item.id"
                  :label="item.name+item.phone+item.account"
                  :value="item.id"
                >
                    <span>{{ item.name }}-{{ item.phone }}</span>
                    <span style="float: right;color: #c7c7c7;">{{ item.account }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品种合约:" prop="instrumentCode">
              <el-input v-model="dialogFormData.instrumentCode" placeholder="请输入品种合约"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="练习日期:" prop="tradeDate">
              <el-date-picker
                v-model="dialogFormData.tradeDate"
                type="date"
                placeholder="请选择练习日期"
                size="small"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易归属:" prop="tradeDate">
              <el-date-picker
                v-model="dialogFormData.tradeDate"
                type="date"
                placeholder="请选择练习日期"
                size="small"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="买/卖: ">
                <el-select v-model="dialogFormData.offsetFlag" placeholder="请选择买/卖" collapse-tags clearable>
                  <template v-for="item in buyOrSellList" :key="item.id">
                    <el-option :label="item.dicName" :value="item.dicValue" v-if="item.dicValue != 'Sell' && item.dicValue != 'Buy'"/>
                  </template>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="权利金单价:" prop="tradeSinglePrice">
              <el-input-number v-model="dialogFormData.tradeSinglePrice" placeholder="请输入权利金单价"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="练习数量:" prop="tradeVolume">
              <el-input-number v-model="dialogFormData.tradeVolume" placeholder="请输入练习数量"  clearable/>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="权利金:" prop="premium">
              <el-input-number v-model="dialogFormData.premium" placeholder="请输入权利金"  clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交易保证金:" prop="tradeMargin">
              <el-input-number v-model="dialogFormData.tradeMargin" :disabled="dialogFormData.offsetFlag!='SellCall'" placeholder="请输入交易保证金"  clearable/>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer" style="text-align: right;margin-right: 7%;">
          <el-button  size="small" @click="visable = false"> 取消 </el-button>
          <el-button  size="small" type="primary" @click="save"> 保存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { updateTrade, addTrade, getTradeByID } from '@/api/oss/tradeManage.js'
import { getAll } from '@/api/oss/student.js'
import { selectDictionaryByTypeCode } from '@/api/system/dictionary.js'
import { getUserInfo } from '@/utils/localStorage.js'
var currentDate = new Date(); // 获取当前时间
const year = currentDate.getFullYear();
const month = currentDate.getMonth() + 1; // 注意月份是从0开始计数的，需要加1
const day = currentDate.getDate();
var currentHour = currentDate.getHours(); // 获取当前小时
currentHour = currentHour < 10 ? '0'+currentHour : currentHour;
var currentMinute = currentDate.getMinutes(); // 获取当前分钟
currentMinute = currentMinute < 10 ? '0'+currentMinute : currentMinute;
var currentSecond = currentDate.getSeconds(); // 获取当前秒钟
currentSecond = currentSecond < 10 ? '0'+currentSecond : currentSecond;
// (修改和新增共用一个弹窗时,this.$refs['xxx'].resetFields()函数失效, 因此使用这种方式重置表单数据)
let orgDialogFormData = {
        id: undefined,
        tradeDate: year+"-"+month+"-"+(day < 10?'0'+day:day),
        tradeTime:currentHour+":"+currentMinute+":"+currentSecond,
        instrumentCode:"",
        varietyCode:'',
        studentID:getUserInfo().studentID,
        offsetFlag:"",
        price:"",
        volume:"",
        remark:"",
        optionType:"",
        strikePrice:"",
        tradeSinglePrice:undefined,
        tradeVolume:1,
        commission:0,
        premiumAveragePrice:"",
        todaySettlementPrice:"",
        yesterdaySettlementPrice:"",
        tradeCode:"",
        serialNumber:"",
        premium:undefined,
        taxFlag:0,
        tradeMargin:undefined,
      }
      console.log("--------",orgDialogFormData);
export default {
  data() {
    return {
      title:"",
      visable: false,
      dialogFormData: { ...orgDialogFormData},
      dialogFormRules: {
        account: [
          { required: true, message: '请选择学员信息', trigger: ['blur', 'change'] },
        ]
      },
      // 学员列表
      studentList:[],
      // 期权类型列表
      optionTypeList:[],
      //买卖方向列表
      buyOrSellList:[],
    }
  },
  methods: {
    openModal(id) {
      console.log("openModal=",id);
      this.visable = true
      this.$nextTick(() => {
        this.dialogFormData = {...orgDialogFormData}
        if (this.isNotBlank(id)) {
          this.title = '修改交易信息'
          // 获取修改的信息
          getTradeByID({ 'id': id }).then(res => {
            console.log("修改返回值=",res);
            if (res.code === 1) {
              this.dialogFormData = {...res.data}
            }
          })
        } else {
          this.title = '新增交易信息'
        }
        // 获取所有学员信息
        getAll({}).then(res=>{
          if (res.code == 1) {
            this.studentList = res.data
          }
        })
        selectDictionaryByTypeCode({"dicTypeCode":"optionType"}).then(res=>{
            if (res.code == 1) {
                this.optionTypeList = res.data
            }
        })
        selectDictionaryByTypeCode({"dicTypeCode":"BuyOrSell"}).then(res=>{
            if (res.code == 1) {
                this.buyOrSellList = res.data
            }
        })
      })
    },
    
    // 保存
    save() {
      this.$refs['addRef'].validate((valid) => {
        if (valid) {
          if (this.isBlank(this.dialogFormData.id)) {
            addTrade(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          } else {
            updateTrade(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          }
          

        }
      })
    },

  }
}
</script>
<style scoped>
  :deep(.el-form-item__content) {
    display: inline-block;
    text-align: left;
    padding-right: 10%;
  }
  :deep(.el-drawer__title) {
    text-align: left;
  }
  :deep(.el-input),
  :deep(.el-input-number--small),
  :deep(.el-select) {
    width: 100%;
  }
</style>
